package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.article.IArticleClient;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.StatisticsDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: StatisticsServiceImpl
 * Description:
 * date: 2023/9/1 0001 15:27
 *
 * @author zHan
 */
@Service
@Slf4j
@Transactional
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private IArticleClient articleClient;
    /**
     * 统计数量
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseResult newsDimension(String beginDate, String endDate) {
        /*//类型转换   字符串转换为date类型
        Date beginDateTime = DateUtils.stringToDate(beginDate);
        Date endDateTime = DateUtils.stringToDate(endDate);*/
        // 用户是否登录
        WmUser wmUser = WmThreadLocalUtil.getUser();
        if (wmUser == null) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer wmUserId = wmUser.getId();
        // 创建Map集合
        Map<String, String> map = new HashMap<>();
        map.put("wmUserId", JSON.toJSONString(wmUserId));
        map.put("beginDate", JSON.toJSONString(beginDate));
        map.put("endDate", JSON.toJSONString(endDate));
        return articleClient.getNewsDimension(map);
    }

    /**
     * 查询统计内容
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult newsPage(StatisticsDto dto) {
        // 检查参数
        dto.checkParam();
        /*//类型转换   字符串转换为date类型
        Date beginDateTime = DateUtils.stringToDate(dto.getBeginDate());
        Date endDateTime = DateUtils.stringToDate(dto.getEndDate());*/
        // 用户是否登录
        WmUser wmUser = WmThreadLocalUtil.getUser();
        if (wmUser == null) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer wmUserId = wmUser.getId();
        // 创建Map集合
        Map<String, String> map = new HashMap<>();
        map.put("wmUserId", JSON.toJSONString(wmUserId));
        map.put("beginDate", JSON.toJSONString(dto.getBeginDate()));
        map.put("endDate", JSON.toJSONString(dto.getEndDate()));
        map.put("dto", JSON.toJSONString(dto));
        return articleClient.getNewPage(map);
    }
}
